package com.lplb.common.swagger.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 *   swagger2 配置
 *
 * @author ws
 * @mail 1720696548@qq.com
 * @date 2020/2/24 0024 12:29
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
//    swagger2：
//      basePackage
//       title
//       description
//        version
//        author
//       url
//      email

    //扫包路径
    @Value("${swagger2.basePackage:com}")
    private String basePackage;
    //文档名称
    @Value("${swagger2.title:接口文档Title}")
    private String title;
    //文档描叙
    @Value("${swagger2.description:Restfun-API 接口文档}")
    private String description;
    //版本号
    @Value("${swagger2.version:1.0.0}")
    private String version;
    //作者
    @Value("${swagger2.author:王松}")
    private String author;
    //预览地址
    @Value("${swagger2.url:http://127.0.0.1}")
    private String url;
    //邮箱
    @Value("${swagger2.description:1720696548@qq.com}")
    private String email;


    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(getParameters())
                .apiInfo(apiInfo())
                .select()
                //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))//扫描全部
                .apis(RequestHandlerSelectors.basePackage(basePackage))                  //指定包 com.coupon.api.service
                .paths(PathSelectors.any()).build().useDefaultResponseMessages(false);
    }

    /**
     * 获取swagger ApiInfo
     *
     * @return
     */
    ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .termsOfServiceUrl(url)
                .version(version)
                .contact(new Contact(author, url, email))
                .build();
    }

    /**
     *  添加默认参数
     *
     * @return java.util.List<springfox.documentation.service.Parameter>
     * @author ws
     * @mail 1720696548@qq.com
     * @date 2020/3/2 0002 12:01
     */
    private List<Parameter> getParameters() {
        //
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name("TOKEN").description("用户令牌").modelRef(new ModelRef("string")).parameterType("header").scalarExample("token").required(true).build();

        // ParameterBuilder tokenPar2 = new ParameterBuilder();
        // tokenPar2.name("CLIENT_TYPE").description("客户端类型").modelRef(new ModelRef("string")).parameterType("header").scalarExample("producer").required(true).build();

        List<Parameter> pars = new ArrayList<>();
        pars.add(tokenPar.build());
        //   pars.add(tokenPar2.build());
        return pars;
    }
}
